package com.sdp.lease.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.db.l_tenant_apply_info.entity.LTenantApplyInfo;
import com.sdp.lease.db.l_tenant_apply_info.service.ILTenantApplyInfoService;
import com.sdp.lease.db.l_tenant_info.entity.LTenantInfo;
import com.sdp.lease.db.l_tenant_info.service.ILTenantInfoService;
import com.sdp.lease.utils.CurrentLoginEmp;
import com.sdp.lease.utils.LeaseConstants;

import cn.hutool.core.bean.BeanUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-25
 */
@RestController
@RequestMapping("/lTenantApplyInfo")
public class LTenantApplyInfoController {

	@Autowired
	private Query query;
	
	@Autowired
	private ILTenantApplyInfoService iLTenantApplyInfoService;
	
	@Autowired
	private ILTenantInfoService iLTenantInfoService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iLTenantApplyInfoService, LTenantApplyInfo.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody LTenantApplyInfo tenantApplyInfo) {
		if(tenantApplyInfo==null) {
			return Result.error("申请息为空");
		}
		if(StringUtils.isEmpty(tenantApplyInfo.getTenantAccountNo())) {
			return Result.error("求租人未登录");
		}
		if(StringUtils.isEmpty(tenantApplyInfo.getTenantType()) || StringUtils.isEmpty(tenantApplyInfo.getTenantContact()) || StringUtils.isEmpty(tenantApplyInfo.getTenantContactWay())) {
			return Result.error("求租人信息不完整");
		}
		//根据账号查询求租人是否已经申请过
		int count = iLTenantInfoService.queryTenantInfoIsExist(tenantApplyInfo);
		if(count>0) {
			return Result.error("申请信息已存在，不能重复申请");
		}
		if(LeaseConstants.求租人类型_个人.getCode().equals(tenantApplyInfo.getTenantType())) {
			if(StringUtils.isEmpty(tenantApplyInfo.getTenantCardCode()) || StringUtils.isEmpty(tenantApplyInfo.getTenantCardPic())) {
				return Result.error("求租人身份证信息不完整");
			}
			
		}else if(LeaseConstants.求租人类型_企业.getCode().equals(tenantApplyInfo.getTenantType())) {
			if(StringUtils.isEmpty(tenantApplyInfo.getTenantUscc()) || StringUtils.isEmpty(tenantApplyInfo.getTenantUsccPic())) {
				return Result.error("求租人企业信息不完整");
			}
		}else {
			return Result.error("求租人类型不合法");
		}
		tenantApplyInfo.setApplyState(LeaseConstants.申请状态_申请中.getCode());
		tenantApplyInfo.setApplyTime(LocalDateTime.now());
		iLTenantApplyInfoService.save(tenantApplyInfo);
		return Result.ok();
	}
	
	/**
	 * 审批
	 * @param keyId
	 * @param applyState
	 * @return
	 */
	@GetMapping(value = "/approval")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> approval(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="applyState",required=true) String applyState) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("审批信息不完整");
		}
		LTenantApplyInfo applyInfo = iLTenantApplyInfoService.getById(keyId);
		if(applyInfo==null) {
			return Result.error("申请信息不存在");
		}
		if(!LeaseConstants.申请状态_申请中.getCode().equals(applyInfo.getApplyState())) {
			return Result.error("此申请已经审批过");
		}
		applyInfo.setApplyState(applyState);
		applyInfo.setApproverTime(LocalDateTime.now());
		applyInfo.setApproverNo(currentLoginEmp.getCurrentLoginInfo().getString("empNo"));
		applyInfo.setApproverName(currentLoginEmp.getCurrentLoginInfo().getString("empName"));
		iLTenantApplyInfoService.updateById(applyInfo);
		if(LeaseConstants.申请状态_申请通过.getCode().equals(applyState)) {
			LTenantInfo tenantInfo = BeanUtil.copyProperties(applyInfo, LTenantInfo.class);
			tenantInfo.setCreateTime(LocalDateTime.now());
			tenantInfo.setKeyId(null);
			iLTenantInfoService.save(tenantInfo);
		}
		
		return Result.ok();
	}
}

